package com.qiche.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.mapper.VoucherRepositoryMapper;
import com.qiche.model.VoucherPackage;
import com.qiche.model.VoucherRepository;
import com.qiche.service.VoucherPackageService;
import com.qiche.service.VoucherRepositoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("voucherRepositoryService")
public class VoucherRepositoryServiceImpl extends BaseService<VoucherRepository> implements VoucherRepositoryService {
    @Resource
    private VoucherRepositoryMapper mapper;

    @Override
    public PageInfo<VoucherRepository> selectByPage(VoucherRepository repository, int page, int limit) {
        Example example = new Example(VoucherRepository.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(repository.getName())) {
            criteria.andLike("name", "%" + repository.getName() + "%");
        }
        if (repository.getMerchantId()!=null) {
            criteria.andEqualTo("merchantId", repository.getMerchantId());
        }
        PageHelper.startPage(page, limit);
        List<VoucherRepository> reposList = selectByExample(example);
        return new PageInfo<>(reposList);
    }

    @Override
    public List<VoucherRepository> selectByMerchantId(Integer merchantId) {
        Example example = new Example(VoucherRepository.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("merchantId", merchantId);
        List<VoucherRepository> reposList = selectByExample(example);
        return reposList;
    }

    @Override
    public int updateQuantity(int quantityOld, Integer quantity,Integer id) {
        return mapper.updateQuantity(quantityOld,quantity,id);
    }
}
